package logging

import (
	"fmt"
	"github.com/mrminglang/tools/paths"
	"github.com/zhan3333/glog"
	"server/src/common"
	"server/src/storage"
	"time"
)

var disableBootScreenLog bool

func DisableBootScreenLog() {
	disableBootScreenLog = true
}

// 日志配置文件
var Logging = struct {
	Channels map[string]glog.Log
	Default  string
}{
	Channels: map[string]glog.Log{
		"default": {
			Driver: glog.DAILY,
			Path:   paths.FullStoragePath(storage.GetStoragePath(), "logs/def/def.log"),
			Level:  glog.DebugLevel,
			Days:   5,
		}, // 默认日志
		"db": {
			Driver: glog.DAILY,
			Path:   paths.FullStoragePath(storage.GetStoragePath(), "logs/db/db.log"),
			Level:  glog.DebugLevel,
			Days:   5,
		}, // 数据库日志
		"accesscenterd": {
			Driver: glog.DAILY,
			Path:   paths.FullStoragePath(storage.GetStoragePath(), "logs/accesscenterd/accesscenterd.log"),
			Level:  glog.DebugLevel,
			Days:   5,
		}, // accesscenterd日志
		"apid": {
			Driver: glog.DAILY,
			Path:   paths.FullStoragePath(storage.GetStoragePath(), "logs/apid/apid.log"),
			Level:  glog.DebugLevel,
			Days:   5,
		}, // apid日志
		"datad": {
			Driver: glog.DAILY,
			Path:   paths.FullStoragePath(storage.GetStoragePath(), "logs/datad/datad.log"),
			Level:  glog.DebugLevel,
			Days:   5,
		}, // datad日志
		"geod": {
			Driver: glog.DAILY,
			Path:   paths.FullStoragePath(storage.GetStoragePath(), "logs/geod/geod.log"),
			Level:  glog.DebugLevel,
			Days:   5,
		}, // geod日志
		"gpsprocessord": {
			Driver: glog.DAILY,
			Path:   paths.FullStoragePath(storage.GetStoragePath(), "logs/gpsprocessord/gpsprocessord.log"),
			Level:  glog.DebugLevel,
			Days:   1,
		}, // gpsprocessord日志
		"gpsprocessor2d": {
			Driver: glog.DAILY,
			Path:   paths.FullStoragePath(storage.GetStoragePath(), "logs/gpsprocessor2d/gpsprocessor2d.log"),
			Level:  glog.DebugLevel,
			Days:   1,
		}, // gpsprocessor2d日志
		"mileaged": {
			Driver: glog.DAILY,
			Path:   paths.FullStoragePath(storage.GetStoragePath(), "logs/mileaged/mileaged.log"),
			Level:  glog.DebugLevel,
			Days:   5,
		}, // mileaged日志
		"monitord": {
			Driver: glog.DAILY,
			Path:   paths.FullStoragePath(storage.GetStoragePath(), "logs/mileaged/monitord.log"),
			Level:  glog.DebugLevel,
			Days:   5,
		}, // monitord日志
		"parse": {
			Driver: glog.DAILY,
			Path:   paths.FullStoragePath(storage.GetStoragePath(), "logs/parse/parse.log"),
			Level:  glog.DebugLevel,
			Days:   5,
		}, // parse日志
		"parsedatad": {
			Driver: glog.DAILY,
			Path:   paths.FullStoragePath(storage.GetStoragePath(), "logs/parsedatad/parsedatad.log"),
			Level:  glog.DebugLevel,
			Days:   5,
		}, // parsedatad日志
		"receiverd": {
			Driver: glog.DAILY,
			Path:   paths.FullStoragePath(storage.GetStoragePath(), "logs/receiverd/receiverd.log"),
			Level:  glog.DebugLevel,
			Days:   2,
		}, // receiverd日志
		"tiredataprocessord": {
			Driver: glog.DAILY,
			Path:   paths.FullStoragePath(storage.GetStoragePath(), "logs/tiredataprocessord/tiredataprocessord.log"),
			Level:  glog.DebugLevel,
			Days:   1,
		}, // tiredataprocessord日志
		"tiredataprocessor2d": {
			Driver: glog.DAILY,
			Path:   paths.FullStoragePath(storage.GetStoragePath(), "logs/tiredataprocessor2d/tiredataprocessor2d.log"),
			Level:  glog.DebugLevel,
			Days:   1,
		}, // tiredataprocessor2d日志
		"uploadcenterd": {
			Driver: glog.DAILY,
			Path:   paths.FullStoragePath(storage.GetStoragePath(), "logs/uploadcenterd/uploadcenterd.log"),
			Level:  glog.DebugLevel,
			Days:   5,
		}, // uploadcenterd日志
		"pushd": {
			Driver: glog.DAILY,
			Path:   paths.FullStoragePath(storage.GetStoragePath(), "logs/pushd/pushd.log"),
			Level:  glog.DebugLevel,
			Days:   2,
		}, // pushd日志
		"push3d": {
			Driver: glog.DAILY,
			Path:   paths.FullStoragePath(storage.GetStoragePath(), "logs/push3d/push3d.log"),
			Level:  glog.DebugLevel,
			Days:   2,
		}, // push3d日志
		"pushclientd": {
			Driver: glog.DAILY,
			Path:   paths.FullStoragePath(storage.GetStoragePath(), "logs/pushclientd/pushclientd.log"),
			Level:  glog.DebugLevel,
			Days:   2,
		}, // pushclient日志
		"protocol": {
			Driver: glog.DAILY,
			Path:   paths.FullStoragePath(storage.GetStoragePath(), "logs/protocol/protocol.log"),
			Level:  glog.DebugLevel,
			Days:   5,
		}, // protocol日志
	},
	Default: "default",
}

// LogBootInfo 详情
func LogBootInfo(info string) {
	datetime := time.Now().Format(common.Layout)
	glog.Def().Infof("[%s] boot: %s", datetime, info)
	if !disableBootScreenLog {
		fmt.Printf("[%s] boot: %s\n", datetime, info)
	}
}

// LogBootPanic 宕机
func LogBootPanic(msg string, err error) {
	datetime := time.Now().Format(common.Layout)
	if !disableBootScreenLog {
		fmt.Printf("[%s] boot: %s: %+v\n", datetime, msg, err)
	}
	glog.Def().WithError(err).Panicf("[%s] boot: %s: %+v", datetime, msg, err)
}

// LogBoot 日志启动
func LogBoot() {
	glog.DefLogChannel = Logging.Default
	glog.LogConfigs = Logging.Channels
	glog.LoadChannels()
	LogBootInfo("logging module init")
}
